package controller;

import domain.Jobholder;
import domain.Resume;
import domain.Work;
import service.*;
import service.impl.JobholderServiceImpl;
import service.impl.RecruiterServiceImpl;
import service.impl.ResumeServiceImpl;
import service.impl.WorkServiceImpl;
import ui.*;
import util.CheckUtil;
import util.InputUtil;
import util.PrintUtil;
import util.SessionUtil;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

public final class JobholderController {
    private JobholderController() {
    }
    private static final JobholderService jobholderService = JobholderServiceImpl.getINSTANCE();
    private static final RegisterService registerService = JobholderServiceImpl.getINSTANCE();
    private static final RecruiterService recruiterService = RecruiterServiceImpl.getINSTANCE();
    private static final ResumeService resumeService = ResumeServiceImpl.getINSTANCE();
    private static final WorkService workService = WorkServiceImpl.getINSTANCE();

    /**
     * 求职者模块入口
     */
    public static void run() {
        /*
         * 求职者入口
         * 1、登录
         * 2、注册
         * 0、退出模块
         */
        while (true){
            WelcomeUI.firstView("求职者");
            switch (InputUtil.getInputInteger("请输入选项",0,2)){
                case 1:
                    //选项1,登录
                    login();
                    break;
                case 2:
                    //选项2,注册
                    register();
                    break;
                case 0:
                    //返回上级调用该方法的位置，方法结束
                    return;
            }
        }
    }

    /**
     * 求职者登录
     */
    public static void login() {
        /*
         * 求职者登录
         * 1、输入手机号
         * 2、判断手机号，格式错误（请重新输入），格式正确继续
         * 3、输入密码
         * 4、判断密码是否正确，登录失败请重试返回上一级，密码正确登录成功
         * 5、index() 进行接下来操作
         */
        PrintUtil.printLine();
        PrintUtil.printString("求职者登录");
        PrintUtil.printLine();
        String tel=InputUtil.getInputText("请输入登录手机号");
        while (!CheckUtil.checkTel(tel)) {
            System.out.println("输入的手机号格式错误!");
            tel = InputUtil.getInputText("请重新输入:");
        }
        String pw=InputUtil.getInputText("请输入密码:");
        Jobholder jobholder=jobholderService.login(tel,pw);
        if(jobholder == null){
            PrintUtil.printString("密码输入错误，请重新登录");
            return;
        }
        PrintUtil.printString("登录成功!");
        SessionUtil.setJobholder(jobholder);
        index();
    }

    /**
     * 求职者注册
     */
    public static void register() {
        /*
         * 求职者注册
         * 1、输入手机号，判断手机号，格式错误（请重新输入），格式正确继续
         * 2、输入密码，判断密码是否正确，密码正确继续
         * 3、输入名称，判断名称是否正确，格式错误（请重新输入），格式正确继续
         * 4、输入性别数字
         * 5、输入个性签名
         * 6、根据注册返回值判断，为1时注册成功，否则失败，返回上一级
         */
        PrintUtil.printLine();
        PrintUtil.printString("求职者注册");
        PrintUtil.printLine();
        Jobholder jobholder=new Jobholder();
        jobholder.setTel(RegisterController.getTel(registerService));
        jobholder.setPassword(RegisterController.getPassword());
        jobholder.setName(RegisterController.getName(registerService));
        jobholder.setSex(InputUtil.getInputInteger("请输入性别(男:0 女:1):", 0, 1));
        jobholder.setSignature(InputUtil.getInputText("请输入个性签名:"));
        //
        if (jobholderService.register(jobholder) > 0) {
            PrintUtil.printString("注册成功！");
        } else {
            PrintUtil.printString("注册失败，请重试！");
        }
    }

    /**
     * 登录后求职者首页
     */
    private static void index() {
        /*
         * 求职者首页
         * 1、求职广场 searchWorkByJobholder();
         * 2、个人简历 ResumeController.run();
         * 3、个人信息 managePersonInformation();
         * 4、查询通过简历 passStateResume();
         * 0、退出登录
         */
        while(true) {
            JobholderUI.index();
            switch (InputUtil.getInputInteger("请输入选项",0,4)){
                case 1:
                    searchWorkByJobholder();
                    break;
                case 2:
                    ResumeController.run();
                    break;
                case 3:
                    managePersonInformation();
                    break;
                case 4:
                    passStateResume();
                    break;
                case 0:
                    SessionUtil.setJobholder(null);
                    PrintUtil.printString("退出成功!");
                    return;
            }
        }
    }

    public static void managePersonInformation() {
        /*
         * JobholderUI.personInformation();
         * 请选择需要修改的项
         * 1、修改名称
         * 2、修改电话号码，先输入原电话号码，判断电话号码是否一致，一致再输入新电话号码
         * 3、修改密码，先输入原密码，判断密码是否一致，一致再输入新密码
         * 4、修改性别
         * 5、修改个性签名
         * 0、返回上级
         * JobholderService.resetJobholder();
         */
        Jobholder jobholder=SessionUtil.getJobholder();
        while(true){
            //显示当前jobholder的所有信息
            JobholderUI.personInformation(jobholder);
            switch (InputUtil.getInputInteger("请输入需要修改的选项",0,5)){
                case 1:
                    //修改名称
                    jobholder.setName(RegisterController.getName(registerService));
                    break;
                case 2:
                    //修改电话号码
                    if(!Objects.equals(jobholder.getTel(),InputUtil.getInputText("请输入原电话号码:"))){
                        PrintUtil.printString("输入电话号码有误，请重试!:");
                        break;
                    }
                    PrintUtil.printString("请输入新电话号码:");
                    jobholder.setTel(RegisterController.getTel(registerService));
                    break;
                case 3:
                    //修改密码
                    if(!Objects.equals(jobholder.getPassword(),InputUtil.getInputText("请输入原密码:"))){
                        PrintUtil.printString("密码错误！请重试！");
                        break;
                    }
                    jobholder.setPassword(RegisterController.getPassword());
                    break;
                case 4:
                    //修改性别
                    PrintUtil.printString("请输入新性别(男:0 女:1):");
                    jobholder.setSex(InputUtil.askForInt());
                case 5:
                    //修改个性签名
                    jobholder.setSignature(InputUtil.getInputText("请输入新个性签名："));
                    break;
                case 0:
                    return;
            }
            if(jobholderService.resetJobholder(jobholder) == 1) PrintUtil.printString("更新信息成功");
            else PrintUtil.printString("更新信息失败");
        }
    }

    /**
     * 求职广场
     */
    public static void searchWorkByJobholder() {
        /*
         * 1、显示搜索界面JobholderUI.searchWorkByJobholder(); 四种搜索模式
         * List<Work> works = Collections.emptyList();
         * 2、通过输入不同的数字返回不同的搜索结果保存到works
         * 3、显示工作列表WrokUI.showWorkList(works);
         * 4、读取求职者输入，显示对应的详细情况workDetail(work.get(input - 1))
         */
        PrintUtil.printLine();
        PrintUtil.printString("显示全部搜索界面");
        PrintUtil.printLine();
        List<Work> works = Collections.emptyList();
        JobholderUI.searchWorkByJobholder();
        int choose = InputUtil.getInputInteger("请输入搜索工作的编号：", 0, 4);
        if(choose == 0) return;
        switch (choose) {
            case 1:
                works = workService.selectAll();
                break;
            case 2:
                String name = InputUtil.getInputText("请输入要搜索的公司");
                works = workService.selectByName(name);
                break;
            case 3:
                String place = InputUtil.getInputText("请输入要搜索的地点");
                works = workService.selectByPlace(place);
                break;
            case 4:
                String information = InputUtil.getInputText("请输入要搜索的工作名称");
                works = workService.selectByInformation(information);
                break;
        }
        while(true) {
            WorkUI.showWorkList(works);
            int check = InputUtil.getInputInteger("请输入查看具体工作的编号：", 0, works.size());
            if(check == 0) return;
            Work work = works.get(check - 1);
            WorkUI.showDetailWork(work);
            JobholderUI.showJobholderOperator();
            switch (InputUtil.getInputInteger("请输入投递选项(投递:1 不投递:0):", 0,1)) {
                case 1:
                    sendResume(work);
                    return;
                case 0:
                    return;
            }
        }
    }

    /**
     * 投递简历
     */
    public static void sendResume(Work work) {

        /*
         * Jobholder jobholder = SessionUtil.getJobholder();
         * List<Resume> resumes = Collections.emptyList();
         * resumes = resumeService.getByJobholderId(jobholder.getId());
         * 1、判断简历是否存在，不存在输出(你还没有简历，请添加简历)返回，存在继续
         * 2、显示简历列表ResumeUI.showResumeList(resumes);简历id+姓名
         * 3、读取求职者输入，显示对应的详细情况resumeDetail(resume.get(input - 1), workService.getRecruiterId())
         */
        Jobholder jobholder = SessionUtil.getJobholder();
        List<Resume> resumes = Collections.emptyList();
        resumes = resumeService.selectByJobholderId(jobholder.getId());
        if(resumes == null){
            PrintUtil.printString("你好没有简历，请先添加简历");
            return;
        }
        ResumeUI.showResumeList(resumes);
        int check = InputUtil.getInputInteger("请输入待投递的简历编号：", 0, resumes.size());
        if(check == 0) return;
        Resume resume = resumes.get(check - 1);
        ResumeUI.showDetailResume(resume);
        JobholderUI.showJobholderOperator();
        switch (InputUtil.getInputInteger("请输入投递选项(投递:1 不投递:0):", 0,1)) {
            case 1:
                if(resume.getRecruiterId() == 0){
                    resume.setRecruiterId(work.getRecruiterId());
                    if(resumeService.resetResume(resume) == 1) PrintUtil.printString("投递成功");
                    else PrintUtil.printString("投递失败");
                }else {
                    PrintUtil.printString("此简历已经投递过");
                }
                break;
            case 0:
                break;
        }
    }

    /**
     * 查询通过简历
     */
    public static void passStateResume() {

        /*
         * 已通过简历
         * Jobholder jobholder = SessionUtil.getJobholder();
         * List<Resume> resumes = Collections.emptyList();
         * resumes = resumeService.getJobholderIdAndPassState(jobholder.get(id), 1);
         * 1、显示简历列表ResumeUI.showResumeList(resumes);简历id+姓名
         */
        Jobholder jobholder = SessionUtil.getJobholder();
        List<Resume> resumes = Collections.emptyList();
        resumes = resumeService.selectByJobholderIdAndPassState(jobholder.getId(), 1);
        if(resumes == null){
            PrintUtil.printString("暂时还没有通过的简历");
            return;
        }
        PrintUtil.printString("通过的简历如下");
        ResumeUI.showResumeList(resumes);
    }
}
